package cn.com.haopy.yue.ai.admin.web;

import cn.com.haopy.yue.ai.admin.annotation.RequiresPermissionsDesc;
import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.core.validator.Order;
import cn.com.haopy.yue.ai.core.validator.Sort;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskStandardCharges;
import cn.com.haopy.yue.ai.db.entity.arisk.HOSAdmin;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskStandardChargesService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Description: TODO
 * @Author DuTao
 * @Date 2025/3/26 9:21
 */
@RestController
@RequestMapping("/admin/standardCharges")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "计费标准管理")
public class AdminAiRiskStandardChargesController {

    @Autowired
    private AIRiskStandardChargesService standardChargesService;

    @RequiresPermissions("admin:standardCharges:list")
    @RequiresPermissionsDesc(menu = {"运营管理", "计费标准"}, button = "查询")
    @GetMapping("/list")
    @ApiOperation("列表")
    public Object list(@RequestParam(required = false) @ApiParam("标题") String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "sort_index") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        Page<AIRiskStandardCharges> pageList = standardChargesService.querySelective(name, page, limit, sort, order);
        return ResponseUtil.okPage(pageList);
    }

    @RequiresPermissions("admin:standardCharges:create")
    @RequiresPermissionsDesc(menu = {"运营管理", "计费标准"}, button = "添加")
    @PostMapping("/create")
    @ApiOperation("新增")
    @Transactional
    public Object create(@RequestBody AIRiskStandardCharges homeLink) {
        Object error = validate(homeLink);
        if (error != null) {
            return error;
        }
        Subject currentUser = SecurityUtils.getSubject();
        HOSAdmin admin = (HOSAdmin) currentUser.getPrincipal();
        homeLink.setCreateBy(admin.getUsername());
        homeLink.setUpdateBy(admin.getUsername());
        boolean isSuccess = standardChargesService.save(homeLink);
        if (!isSuccess) {
            return ResponseUtil.fail("添加失败");
        }
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:standardCharges:update")
    @RequiresPermissionsDesc(menu = {"运营管理", "计费标准"}, button = "修改")
    @PostMapping("/update")
    @ApiOperation("编辑")
    @Transactional
    public Object update(@RequestBody AIRiskStandardCharges homeLink) {

        Subject currentUser = SecurityUtils.getSubject();
        HOSAdmin admin = (HOSAdmin) currentUser.getPrincipal();
        homeLink.setUpdateBy(admin.getUsername());
        if (standardChargesService.updateById(homeLink)) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    @RequiresPermissions("admin:standardCharges:detail")
    @RequiresPermissionsDesc(menu = {"运营管理", "计费标准"}, button = "详情")
    @GetMapping("/detail")
    @ApiOperation("详情")
    @Transactional
    public Object detail(@RequestParam Integer id) {
        AIRiskStandardCharges standardCharges = standardChargesService.getById(id);
        if (Objects.nonNull(standardCharges)) {
            return ResponseUtil.ok(standardCharges);
        }
        return ResponseUtil.fail();
    }


    @RequiresPermissions("admin:standardCharges:delete")
    @RequiresPermissionsDesc(menu = {"运营管理", "计费标准"}, button = "删除")
    @GetMapping("/delete")
    @ApiOperation("删除")
    @Transactional
    public Object delete(@RequestParam Integer id) {
        Boolean bool = standardChargesService.removeById(id);
        if (bool) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail("删除数据失败");
    }

    private Object validate(AIRiskStandardCharges homeLink) {
        if (!StringUtils.hasText(homeLink.getName())) {
            return ResponseUtil.badArgumentValue();
        }
        return null;
    }

    @GetMapping("/listCalculate")
    @ApiOperation("计算公式列表")
    public Object listCalculate() {
        List<Map<String, Object>> treeList = new ArrayList<>();
        List<AIRiskStandardCharges> list = standardChargesService.listCalculate();
        list.forEach(item -> {
            Map<String, Object> treeItem = new HashMap<>();
            treeItem.put("id", item.getId());
            treeItem.put("name", item.getName() + item.getSubTitle());
            treeItem.put("isSingle", item.getIsSingle());
            treeItem.put("isLeaf", true);
            treeList.add(treeItem);
        });
        return ResponseUtil.ok(treeList);
    }
}
